<?php

namespace App\Models\Users;

use App\Models\User;
use DB;
use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{
    protected $fillable = [
        'name', 'display_name', 'description', 'can_delete', 'is_default', 'flag'
    ];

    public function deletePermission()
    {
        $perms = $this->perms;
        foreach ($perms as $item) {
            DB::table('permission_role')
                ->where('permission_id', $item->id)
                ->where('role_id', $this->id)
                ->delete();
        }
    }

    public function checkFlagByUser(User $user)
    {
         foreach ($user->roles as $roles) {
             if ($roles->flag > $this->flag) {
                 return true;
                 break;
             }
             continue;
         }
        return false;
    }
}
